package com.jz.work.core;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;


public class DataSourceExchange implements MethodBeforeAdvice, AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method,
                               Object[] args, Object target) throws Throwable {
        DataSourceContextHolder.clearDataSource();
    }

    @Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        //这里DataSource是自定义的注解，不是java里的DataSource接口  
        if (method.isAnnotationPresent(DataSource.class)) {
            DataSource datasource = method.getAnnotation(DataSource.class);
            if (null == datasource || (null != datasource && null == datasource.name()))
                DataSourceContextHolder.setDataSource(datasource.wohaibao);
            else {
                System.out.println(datasource.name());
                DataSourceContextHolder.setDataSource(datasource.name());
            }
        }

    }
}    